package com.dao;

import java.util.List;
import java.util.Map;

import com.entity.User;
import com.utils.DbUtils;
import com.utils.StringUtils;

public class UserDao implements BaseDao<User>{
    
    DbUtils dbUtils=new DbUtils();
    //添加
    public boolean add(User user) {
	String sql="insert into user(username,password) values(?,?)";
	return dbUtils.executeUpdate(sql, user.getUsername(),user.getPassword());
    }
    //删除
    public boolean delete(Integer id) {
	String sql="delete from user where id=?";
	return dbUtils.executeUpdate(sql, id);
    }
    //单删除和多选删除通用
    public boolean deletebatch(String ids) {
	System.out.println("删除id——>"+ids);
	//多选字符串类型1,2,3  转换成object数组传入
	Object args[]=StringUtils.StringTranObject(ids);
	String sql="delete from user where id in("+StringUtils.StringReplaceSign(ids)+")";
	return dbUtils.executeUpdate(sql,args);
    }
    //编辑
    public boolean edit(User user,Integer id) {
	String sql="update user set username=? where id=?";
	return dbUtils.executeUpdate(sql, user.getUsername(),id);
    }
    //查询所有
    public List<User> findAll() {
	String sql="select * from user";
	return dbUtils.executeQuery(sql);
    }
    //分页
    public List<User> findPageAll(Integer page, Integer limit) {
	String sql="select * from user limit ?,?";
	return dbUtils.executeQuery(sql,(page-1)*limit,limit);
    }
    //单个查询
    public User findById(Integer id) {
	String sql="select * from user where id=?";
	List<Map<String,Object>> listUser=dbUtils.executeQuery(sql, id);
	User user=new User();
	if(!listUser.isEmpty()) {
	    user.setId((Integer) listUser.get(0).get("id"));
	    user.setUsername(listUser.get(0).get("username")+"");
	    user.setPassword(listUser.get(0).get("password")+"");
	    return user;
	}
	return null ;
    }


}
